<%@page import="study.itwill.team5.house.HouseItem"%>
<%@page import="study.itwill.team5.house.House"%>
<%@page import="study.itwill.common.DateTime"%>
<%@page import="study.itwill.team5.admin.AdminItem"%>
<%@page import="study.itwill.jsp.WebHandler"%>
<%@page import="study.itwill.team5.board.CommentItem"%>
<%@page import="study.itwill.team5.board.Comment"%>
<%@page import="study.itwill.team5.member.MemberItem"%>
<%@page import="study.itwill.team5.member.Member"%>
<%@page import="study.itwill.team5.board.BoardItem"%>
<%@page import="study.itwill.team5.board.Board"%>
<%@page import="study.itwill.common.DBHandler"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ page trimDirectiveWhitespaces="true"%>
<%@ include file="../session.jsp"%>
<%@ include file="../dbcon.jsp"%>
<%
	DateTime datetime = DateTime.getInstance();

	int seq = web.getInt("seq");

	if (seq == -1) {
		db.disconnect();
		web.goPage(null, "파라미터가 잘못되었습니다.");
		return;
	}
%>
<%
	
	/** 게시판 데이터 조회 */
	Board board = new Board();
	BoardItem item = board.viewBoard(seq);
	
	// 게시판 카테고리에 대한 ArrayList 받아오기
	BoardCategory category = new BoardCategory();
	ArrayList<BoardCategoryItem> category_list = category.listBoardCategory();
	
	// 회원목록 받아오기
	Member member = new Member();
	ArrayList<MemberItem> member_list = member.listMember(0);
	
	// 민박목록 받아오기
	House house = new House();
	ArrayList<HouseItem> house_list = house.listHouse(0);
	
	// 덧글목록 받아오기
	Comment comment = new Comment();
	ArrayList<CommentItem> comment_list = comment.listComment(1, "" + seq);
	
	if (item == null) {
		db.disconnect();
		web.goPage(null, "데이터 조회에 실패했습니다. 잠시후 다시 시도해 주세요.");
		return;
	}
%>
<!DOCTYPE html>
<html lang="ko">
<head>
<%@ include file="../head.jsp"%>
<!-- Javascript -->
<script src="/Team-Project/assets/js/jquery.js"></script>
<script src="/Team-Project/assets/js/bootstrap.min.js"></script>
<!-- 형식검사 클래스 참조 -->
<script src="/Team-Project/assets/js/string.js"></script>
</head>
<body>
	<%@ include file="../menu.jsp"%>
	<div class="container">
		<div class="page-header">
			<h1>게시글 보기</h1>
		</div>

		<form id="frmBoardView" class="form-horizontal" role="form"
			method="post" action="">

			<!-- 게시글 일련번호 자동입력 -->
			<input type="hidden" name="board_seq" value="<%=seq%>">
			<input type="hidden" name="house_seq" value="<%=item.getHouse_seq()%>">
			<input type="hidden" name="grade" value="<%=item.getGrade()%>">


			<fieldset>

				<!-- 입력양식 -->
				<div class="form-group">
					<label for="board_category_seq" class="col-md-2 control-label">게시판
						카테고리</label>
					<div class="col-md-10">
						<p class="form-control-static"><%=item.getCategory_name()%></p>
					</div>
				</div>

				<div class="form-group">
					<label for="member_seq" class="col-md-2 control-label">회원</label>
					<div class="col-md-10">
						<p class="form-control-static"><%=item.getUser_name()%></p>
					</div>
				</div>

				<div class="form-group">
					<label for="title" class="col-md-2 control-label">제목</label>
					<div class="col-md-10">
						<p class="form-control-static"><%=item.getTitle()%></p>
					</div>
				</div>

				<div class="form-group">
					<label for="hit" class="col-md-2 control-label">조회수</label>
					<div class="col-md-10">
						<p class="form-control-static"><%=item.getHit()%></p>
					</div>
				</div>
				
				<%
				if (category_list != null) {
					for (int i=0; i<category_list.size(); i++) {
						BoardCategoryItem cate_item = category_list.get(i);
						if (cate_item.getSeq() == item.getBoard_category_seq() && item.getCategory_name().indexOf("후기") > -1) {
				%>
				<div class="form-group">
					<label for="house_seq" class="col-md-2 control-label">민박</label>
					<div class="col-md-10">
						<p class="form-control-static"><% for (int j=0; j<house_list.size(); j++) {
								HouseItem house_item = house_list.get(j);
								if (item.getHouse_seq() == house_item.getSeq()) {
									%><%=house_item.getName()%><%
								}
							} %></p>
					</div>
				</div>
				
				<div class="form-group">
					<label for="grade" class="col-md-2 control-label">평점</label>
					<div class="col-md-10">
						<p class="form-control-static"><%=item.getGrade()%></p>
					</div>
				</div>
				<%			
						}
					}
				}
				%>

				<div class="form-group">
					<label for="content" class="col-md-2 control-label">내용</label>
					<div class="col-md-10">
						<p class="form-control-static"><%=item.getContent()%></p>
					</div>
				</div>

				<div class="form-group">
					<label for="reg_date" class="col-md-2 control-label">등록일자</label>
					<div class="col-md-10">
						<p class="form-control-static"><%= datetime.getDateTimeString(item.getReg_date()) %></p>
					</div>
				</div>

				<div class="form-group">
					<label for="edit_date" class="col-md-2 control-label">수정일자</label>
					<div class="col-md-10">
						<p class="form-control-static"><%= datetime.getDateTimeString(item.getEdit_date()) %></p>
					</div>
				</div>

				<div class="form-group">
					<div class="col-md-offset-2 col-md-10">
						<div class="row">
							<div class="col-md-4">
								<a
									href="listBoard.jsp?category=<%=item.getBoard_category_seq()%>"
									class="btn btn-primary btn-block">목록보기</a>
							</div>
							<div class="col-md-4">
								<button type="submit" id="edit" formaction="editBoard.jsp"
									formmethod="post" class="btn btn-info btn-block">수정하기</button>
							</div>
							<div class="col-md-4">
								<button type="submit" id="delete" formaction="deleteBoardOk.jsp"
									formmethod="post" class="btn btn-danger btn-block">삭제하기</button>
							</div>
						</div>
					</div>
				</div>

			</fieldset>
		</form>

		<hr />

		<div class="col-md-offset-2 col-md-10">
			<ul class="media-list">
				<%
			if (comment_list.size() > 0) {
				for (int i=0; i<comment_list.size(); i++) {
					CommentItem comment_item = comment_list.get(i);
			%>
				<li class="media">
					<div class="media-body">
						<h4 class="media-heading"><%=comment_item.getUser_name()%>&nbsp;&nbsp;&nbsp;
							<small><%= datetime.getDateTimeString(item.getEdit_date()) %>&nbsp;&nbsp;&nbsp;
								<a href="#" id="editcomment"
								onclick="window.open('editComment.jsp?comment_seq=<%=comment_item.getSeq()%>', '', 'width=1000, height=250, scrollbars=no, toolbar=no, menubar=no, status=no, location=no'); return false;">수정</a>&nbsp;/&nbsp;
								<a
								href="deleteCommentOk.jsp?comment_seq=<%=comment_item.getSeq()%>&board_seq=<%=seq%>"
								id="delete<%=comment_item.getSeq()%>">삭제</a></small>
						</h4>
						<script type="text/javascript">
								$(function() {
									// id 속성이 "delete"인 요소에 대한 click이벤트
									$("#delete<%=comment_item.getSeq()%>").click(function() {
										// 사용자에게 물어본다.
										if (!confirm("정말 삭제하시겠습니까?")) {
											// 취소를 선택한 경우 처리 둥단
											return false;
										}
									});
								});
							</script>
						<%=comment_item.getComment()%>
					</div>
				</li>
				<%
				}
			}
			%>
			</ul>
		</div>


		<form id="frmCommentAdd" class="form-horizontal" role="form"
			method="post" action="addCommentOk.jsp">
			<fieldset>

				<!-- 게시판 카테고리와 게시글 일련번호 자동입력 -->
				<input type="hidden" name="board_seq" value="<%=seq%>">

				<div class="form-group">
					<div class="col-md-2">
						<select id="member_seq" name="member_seq" class="form-control">
							<option>덧글작성자</option>
							<%
							if (member_list != null) {
								for (int i=0; i<member_list.size(); i++) {
									MemberItem member_item = member_list.get(i);
						%>
							<option value="<%=member_item.getSeq()%>"><%=member_item.getUser_name()%>
								/
								<%=member_item.getEmail1()%>@<%=member_item.getEmail2()%></option>
							<%			
								}
							}
						%>
						</select>
					</div>
					<div class="col-md-9">
						<textarea class="form-control" rows="3" id="comment"
							placeholder="덧글 내용을 입력하세요" name="comment"></textarea>
					</div>
					<div class="col-md-1">
						<button type="submit" id="comment_write"
							formaction="addCommentOk.jsp" formmethod="post"
							class="btn btn-success btn-block">등록</button>
					</div>
				</div>
			</fieldset>
		</form>
	</div>
	<% db.disconnect(); %>
	<script type="text/javascript">
		$(function() {
			// id 속성이 "delete"인 요소에 대한 click이벤트
			$("#delete").click(function() {
				// 사용자에게 물어본다.
				if (!confirm("정말 삭제하시겠습니까?")) {
					// 취소를 선택한 경우 처리 둥단
					return false;
				}
			});
			
			$("#frmCommentAdd").submit(function() {
				// 작성자 선택여부 검사
				if ($("select[name='member_seq'] > option:selected").index() < 1) {
					alert("작성자를 선택하세요.");
					$("select[name='member_seq']").focus();
					return false;
				}
				
				// 덧글 입력여부 검사
				if (!$("textarea[name='comment']").val().isValue()) {
					alert("덧글을 입력하세요.");
					$("input[name='comment']").focus();
					return false;
				}
			});
		});
	</script>

</body>
</html>